package com.project.sianet.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

import com.project.sianet.MainActivity;
import com.project.sianet.R;
import com.project.sianet.data.LibretaArrayAdapter;
import com.project.sianet.entidades.Libreta;

import java.util.ArrayList;
import java.util.List;

public class LibretaDetalleFragment extends Fragment {
    
	public LibretaDetalleFragment() {}
	
	public static final LibretaDetalleFragment newInstance(String login, List<Libreta> libreta, int bim, String titleFrag, int subTitleFrag, String nomBim, int cantBim) {
		LibretaDetalleFragment f = new LibretaDetalleFragment();
		
        Bundle args = new Bundle();
        args.putInt("mBim", bim);
        args.putString("mLog", login);
        args.putParcelableArrayList("mLib", (ArrayList<Libreta>) libreta);
        args.putString("mTit", titleFrag);
        args.putInt("mSubt", subTitleFrag);
        args.putString("mNomB", nomBim);
        args.putInt("mCantBim", cantBim);
        f.setArguments(args);
        
	    return f;
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    	if (container == null) {
	        return null;
	    }
    	
    	View view = inflater.inflate(R.layout.fragment_libreta_lista, container, false);

        List<Libreta> mLibreta = getArguments().getParcelableArrayList("mLib");
    	
    	if (mLibreta != null && mLibreta.size() > 0 && mLibreta.get(0).getCodigoCurso() != -1) {
	    	LibretaArrayAdapter adapter = new LibretaArrayAdapter(view.getContext(), R.layout.mensajes_list, mLibreta, getArguments().getInt("mBim"), getArguments().getInt("mCantBim"));
			ListView lv = (ListView) view.findViewById(R.id.lv_areas);
			lv.setAdapter(adapter);
			
			lv.setOnItemClickListener(new OnItemClickListener() {
				
	            @Override
	            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	            	
	            	if (!getArguments().getString("mNomB").equals(getString(R.string.lblAbrvFinal)))
	            		onSelectedAreaItemClick(parent, view, position, id);
	            }
	        
			});
    	}

        return view;
        
	}

    private void onSelectedAreaItemClick(AdapterView<?> parent, View view, int position, long id) { 
    	LibretaArrayAdapter arrayLibreta = (LibretaArrayAdapter) parent.getAdapter();
    	Libreta lib = arrayLibreta.getItem(position);

    	Fragment newContent = LibretaDetalleCriteriosFragment.newInstance(getArguments().getString("mLog"),
    			getArguments().getInt("mBim"), lib, getArguments().getString("mTit"), getArguments().getInt("mSubt"),
    			getArguments().getString("mNomB"));
    	switchFragment(newContent);
    	
    }
    
    private void switchFragment(Fragment fragment) {
 		if (getActivity() == null)
 			return;
 		
 		if (getActivity() instanceof MainActivity) {
 			MainActivity ma = (MainActivity) getActivity();
 			ma.switchContent(fragment);
 		}
 	}
}
